package jianzhioffer;

import java.util.Deque;
import java.util.Stack;

/**
 * Author: Zhang Dongwei
 * Date: 2023/3/24 20:03
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
 */
public class offer_30_包含main函数的栈 {

    public static void main(String[] args) {
        offer_30_包含main函数的栈 minStack = new offer_30_包含main函数的栈();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        int min = minStack.min();
        System.out.println(min);
        minStack.pop();
        int top = minStack.top();
        System.out.println(top);
        int min1 = minStack.min();
        System.out.println(min1);
    }

    Stack<Integer> A, B;

    public offer_30_包含main函数的栈() {
        A = new Stack<>();
        B = new Stack<>();
    }

    public void push(int x) {
//        A.add(x);
        A.push(x);
        if (B.empty() || B.peek() >= x)
            B.add(x);
    }

    public void pop() {
        if (A.pop().equals(B.peek()))
            B.pop();
    }

    public int top() {
        return A.peek();
    }

    public int min() {
        return B.peek();
    }
}
